<?php

namespace App\Models\MLM;

use App\Models\BaseModel;

class MLM extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'created_at',
        'updated_at',
    ];

    protected $table = 'mlm';
    protected $dateFormat = 'U';

    public function children(){
        return $this->belongsTo('App\Models\User\User', 'user_id', 'user_id');
    }

    public function parent(){
        return $this->belongsTo('App\Models\User\User', 's_parent_id', 'user_id');
    }

    public function getChildren($user_id, $level, $page, $size){
        $data['list'] = $this->where($level.'_parent_id', $user_id)
            ->withOnly('children', ['user_id', 'nickname', 'cover', 'mobile'])
            ->forPage($page, $size+1)
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

    public function getParent($user_id){
        $data = $this->where('user_id', $user_id)
            ->withOnly('parent', ['user_id', 'nickname', 'cover', 'mobile'])
            ->get()
            ->toArray();
        return $this->after($data);
    }
}
